import { Module } from 'vuex'
import { RootState } from '@/store/types'
import { getDataList as fetchStaffDepartmentList } from '@/api/company/department'
import { getDataList as fetchStaffRoleList } from '@/api/company/role'

export interface StaffModuleState {
  department: any[]
  role: any[]
}

const staffModule: Module<StaffModuleState, RootState> = {
  namespaced: true,
  state: () => {
    return {
      department: [],
      role: []
    }
  },
  getters: {
    departmentOptions(store) {
      return store.department.map((item) => {
        return { label: item.name, value: item.id, disabled: item.status === 0 }
      })
    },
    roleOptions(store) {
      return store.role.map((item) => {
        return { label: item.name, value: item.id, disabled: item.status === 0 }
      })
    }
  },
  mutations: {
    changeCategory(state, payload) {
      state.department = payload
    },
    changeRole(state, payload) {
      state.role = payload
    }
  },
  actions: {
    async init({ commit }) {
      // 获取完整部门信息
      const department = (await fetchStaffDepartmentList())?.data?.list ?? []
      commit('changeCategory', department)

      // 获取完整角色信息
      const role = (await fetchStaffRoleList())?.data?.list ?? []
      commit('changeRole', role)
    }
  }
}

export default staffModule
